package com.taxshare.operation.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taxshare.mybatis.pager.PageHelp;
import com.taxshare.mybatis.pager.PageRequest;
import com.taxshare.mybatis.utils.DozerBeanUtil;
import com.taxshare.operation.entity.CodeEntity;
import com.taxshare.operation.mapper.CommonCodeInfoMapper;
import com.taxshare.operation.view.dto.CodeInfoDTO;
import com.taxshare.operation.view.vo.CodeInfoVO;
import com.taxshare.response.beans.Response;
import com.taxshare.tools.util.UUIDUitl;

/**
    * @ClassName: CommonCodeService
    * @Description: (通用码表)
    * @author liupp
    * @date 2021年1月22日
    *
 */
@Service
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED)
public class CodeService extends ServiceImpl<CommonCodeInfoMapper, CodeEntity>{

	/**
     * @Title: queryPageList
     * @Description: (通用码表分页)
     * @param @param query
     * @param @return    参数
     * @return Response<IPage<CommonCodeInfoVO>>    返回类型
     * @throws
  */
 public Response<IPage<CodeInfoVO>> queryPageList(PageRequest<CodeInfoDTO> query){
     QueryWrapper<CodeEntity> queryWrapper = new QueryWrapper<>();
     String collectionName = query.getInfo().getCollectionName();
     if (StringUtils.isNotEmpty(collectionName)) {
         queryWrapper.like("collection_name", collectionName);
     }
     queryWrapper.orderByDesc("create_time");
     IPage<CodeInfoVO> result = PageHelp.siamplePage(this, queryWrapper,query.getPage(),CodeInfoVO.class);
     return Response.success(result);
 }
	
	/**
	    * @Title: queryList
	    * @Description: (查找通用码表列表)
	    * @param @param commonCodeInfoDTO
	    * @param @return    参数
	    * @return Response<List<CommonCodeInfoVO>>    返回类型
	    * @throws
	 */
    public Response<List<CodeInfoVO>> queryList(CodeInfoDTO commonCodeInfoDTO) {
        QueryWrapper<CodeEntity> queryWrapper = new QueryWrapper<>();
        String collectionName = commonCodeInfoDTO.getCollectionName();
        if (StringUtils.isNotEmpty(collectionName)) {
            queryWrapper.like("collectionName", collectionName);
        }
        List<CodeEntity> list = this.list(queryWrapper);
        return Response.success(DozerBeanUtil.mapList(list, CodeInfoVO.class));
    }
    
    /**
        * @Title: add
        * @Description: (新增码表主表)
        * @param @param commonCodeInfoDTO
        * @param @return    参数
        * @return Response    返回类型
        * @throws
     */
    public Response add(CodeInfoDTO commonCodeInfoDTO){
        CodeEntity commonCodeInfo =  DozerBeanUtil.map(commonCodeInfoDTO, CodeEntity.class);
        commonCodeInfo.setUuid(UUIDUitl.UUID32(false));
        //如果不级联,则目标集合为空
        if ("0".equals(commonCodeInfoDTO.getIsCascade())) {
        	commonCodeInfo.setCascadeCollection("");
        } else {
        	commonCodeInfo.setCascadeCollection(commonCodeInfoDTO.getCascadeCollection());
        }
        commonCodeInfo.setCreateTime(new Date());
        commonCodeInfo.setUpdateTime(new Date());
        this.save(commonCodeInfo);
        return Response.success();
    }
    
    /**
        * @Title: edit
        * @Description: (码表主表修改)
        * @param @param commonCodeInfoDTO
        * @param @return    参数
        * @return Response    返回类型
        * @throws
     */
    public Response edit(CodeInfoDTO commonCodeInfoDTO){
    	CodeEntity commonCodeInfo =  DozerBeanUtil.map(commonCodeInfoDTO, CodeEntity.class);
        //如果不级联,则目标集合为空
        if ("0".equals(commonCodeInfoDTO.getIsCascade())) {
        	commonCodeInfo.setCascadeCollection("");
        } else {
        	commonCodeInfo.setCascadeCollection(commonCodeInfo.getCascadeCollection());
        }
        commonCodeInfo.setUpdateTime(new Date());
        return Response.success(this.updateById(commonCodeInfo));
    }
    
    /**
        * @Title: delete
        * @Description: (码表删除)
        * @param @param ids
        * @param @return    参数
        * @return Response    返回类型
        * @throws
     */
    public Response delete(String uuid){
        return Response.success(this.removeById(uuid));
    }
}
